<template>
  <div class="goods-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card" style="margin-top:20px">
      <!-- 搜索区域 -->
      <div style="margin: 15px 0;">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="goodsObj.query" class="iptBox">
              <el-button slot="append" icon="el-icon-search" @click="searchData"></el-button>
            </el-input>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="addGoods">添加商品</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" stripe border style="width: 100%; margin-bottom:20px;">
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="130"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="130"></el-table-column>
        <el-table-column label="操作" width="130">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="dataEdit(scope.row)" icon="el-icon-edit"></el-button>
            <el-button
              size="mini"
              type="danger"
              @click="dataDelete(scope.row.goods_id)"
              icon="el-icon-delete"
              slot="reference"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodsObj.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="goodsObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total1"
      ></el-pagination>
    </el-card>
  </div>
</template>
           
<script>
import { goodsList_api, delGoods_api } from "@/api";
export default {
  created() {
    this.getGoods();
  },
  data() {
    return {
      //表格数据
      tableData: [],
      /* 获取数据列表参数对象 */
      goodsObj: {
        query: "",
        // 当前页码
        pagenum: 1,
        // 每页的数据
        pagesize: 10,
      },
      // 数据总数
      total1: 0,
    };
  },
  methods: {
    //搜索商品
    searchData() {
      this.getGoods();
    },
    //添加商品
    addGoods() {
       this.$router.push('/goods/add')
    },
    //编辑
    dataEdit() {},
    //删除
    dataDelete(id) {
      // const {data:res} = await delGoods_api(id);
      // console.log(res);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then( async () => {
          const {data:res} = await delGoods_api(id)
          this.getGoods();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /* 分页组件提供的事件 */
    handleSizeChange(val) {
      // console.log(val,111);
      //val表示每页多少条
      this.goodsObj.pagesize = val;
      this.getGoods();
    },
    handleCurrentChange(val) {
      // val表示当前的页码
      this.goodsObj.pagenum = val;
      this.getGoods();
      // console.log(val,222);
    },
    async getGoods() {
      const { data: res } = await goodsList_api(this.goodsObj);
      console.log(res);
      //判断请求是否成功
      if (res.meta.status === 200) {
        this.$message({
          showClose: true,
          message: res.meta.msg,
          type: "success",
        });
      } else {
        return this.$message({
          showClose: true,
          message: res.meta.msg,
          type: "success",
        });
      }
      this.tableData = res.data.goods;
      //把数据总数给total1
      this.total1 = res.data.total;
    },
  },
};
</script>
<style scoped>
</style>